package com.my.myrecorder.Fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.my.myrecorder.R;

/**
 * Created by 张英杰 on 2016/10/14.
 */
public class Frag2 extends Fragment {
    //定义回调函数及变量
    protected BackHandlerInterface backHandlerInterface;
    private TextView tv;

    public interface BackHandlerInterface {
        public void setSelectedFragment(Frag2 backHandledFragment);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try{
            backHandlerInterface = (BackHandlerInterface) getActivity();
        }catch (Exception e){
            throw new ClassCastException("Hosting activity must implement BackHandlerInterface");
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        //将自己的实例传出去
        backHandlerInterface.setSelectedFragment(this);
    }

    private boolean mHandledPress = false;
    public boolean onBackPressed(){
        if (!mHandledPress){
            tv.setText("Fragment1 \n 捕捉到了回退事件哦！");
            mHandledPress = true;
            return true;
        }
        return false;
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment,container,false);
        tv = (TextView) v.findViewById(R.id.tv);
        tv.setText("22222222222");
        return v;
    }
}
